<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>新增部门</title>
  <link rel="stylesheet" href="../css/add_department.css"> <!-- 假设对应的样式表 -->
  <script src="../js/jquery.js"></script>
</head>
<body>
<div class="container">
  <h1>新增部门</h1>
  <form id="addDepartmentForm">
    <label for="departmentName">部门名称：</label>
    <input type="text" id="departmentName" name="departname" required><br>

    <label for="leaderId">部门领导ID：</label>
    <input type="text" id="leaderId" name="leaderid" required><br>

    <label for="phone">联系电话：</label>
    <input type="text" id="phone" name="phone" required><br>

    <label for="location">位置：</label>
    <input type="text" id="location" name="location" required><br>

    <button type="submit" class="button">提交</button>
  </form>
</div>
<script>
  $(document).ready(function () {
    $('#addDepartmentForm').submit(function (e) {
      e.preventDefault(); // 阻止表单默认提交行为
      const departmentData = {
        departname: $('#departmentName').val(),
        leaderid: $('#leaderId').val(),
        phone: $('#phone').val(),
        location: $('#location').val()
      };
      $.ajax({
        url: "/addDepartment",
        method: "POST",
        contentType: "application/json",
        data: JSON.stringify(departmentData),
        success: function (res) {
          if (res > 0) {
            alert("新增部门成功！");
            window.location.href = "department.html"; // 返回部门管理页面刷新数据
          } else {
            alert("新增部门失败！");
          }
        },
        error: function () {
          alert("新增部门操作失败，请检查网络或服务端。");
        }
      });
    });
  });
</script>
</body>
</html>